/**
 * Filename: HomeAlbumFragment.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/2/29 17:55
 */
package com.github.postsanf.yinian.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseFragment;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.YunAlbumAdapter;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNAlbumResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.util.LinkedHashMap;
import java.util.List;

public class YunAlbumFragment extends BaseFragment {
    private ListView mListDir;
    private List<YNAlbum> mImageAlbums;
    private Context mContext;
    private YunAlbumAdapter yunAlbumAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        View view = View.inflate(getActivity(), R.layout.yn_fragment_localbum, null);
        mListDir = (ListView) view.findViewById(R.id.rv_local_album);
        getImages(application.getCurID());
        return view;
    }


    private void getImages(int uId) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(uId));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowPhoto))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNAlbumResponse resGroup = gson.fromJson(s, YNAlbumResponse.class);
                        if (resGroup != null && ReqUtils.isSuccess(resGroup.getCode())) {
                            mImageAlbums = YNSingleton.getInstance().getAlbumList();
                            yunAlbumAdapter = new YunAlbumAdapter(mContext, mImageAlbums);
                            mListDir.setAdapter(yunAlbumAdapter);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        Toast.makeText(getActivity(), CommonConstants.YN_NET_LINK, Toast.LENGTH_SHORT).show();
                    }
                });
        liteHttp.executeAsync(req);

    }

}